#include </home/johanna/Programs/dlib-18.6/dlib/svm.h>

inline	
svm_dlib::svm_dlib(const std::string  in_feat_gr_List, const std::string  in_lgocv, const int in_nBins, const std::string in_feat_path)
:feat_gr_List(in_feat_gr_List),lgocv(in_lgocv), nBins(in_nBins), feat_path (in_feat_path)
{
  
}


inline	
void
svm_dlib::run()
{
  
  group_list.load(feat_gr_List);
  group_list.print("Printing...");
  size_traData = group_list.n_rows*24*4; // Leaving one group out. 4 videos per group
  size_testData = group_list.n_rows*1*4; // Leaving one group out. 4 videos per group
  
  
  field<mat> loocv_group; 
  loocv_group.load(lgocv);
  
  
  // Cross Validation
  for (uword it = 0; it<loocv_group.n_rows; it++)
  {
    mat groups =  loocv_group(it,1);
    mat test_group = loocv_group(it,0);
    
    int test_id = test_group(0,0);
    //int test_id = 2;/// CAMBIAR!!!!!!!!!!!!!!!!!!!!!!
    //groups.print("Validation indices");
    //cout << "Validation Group " << test_group(0,0) << endl;
    training(groups);
    //validation(test_id);
    //cout << "end.. press a key";
    //getchar();
    //training
    
  }
  
  //cout << labels << endl;
  //cout << "Filas " << trainingData.rows << endl;
  //cout << "Columns " << trainingData.cols << endl;
  //cout << trainingData.row(sample -1) << endl;
  //cout << hist_tmp.t() << endl;
  //cout << "TrainingData = "<< endl << " "  << trainingData << endl << endl;
  //cout << trainingData << endl;
  //getchar();
  
}



  /// Training the SVM
inline 
void
svm_dlib::training(mat cross_val_groups)
{
  int dim = nBins*2;
  field<vec> fea_group;
  
  // For dlib
  typedef matrix<fvec, dim, 1> sample_type; // Size fvec-->Armadillo
  typedef radial_basis_kernel<sample_type> kernel_type;
  std::vector<sample_type> samples;
  std::vector<double> labels;
    
    
      
  //fmat trainingDataArma;
  //trainingDataArma.set_size(size_traData,nBins*2); //Concatenating two histograms
  //fvec labelsArma;
  //labelsArma.set_size(size_traData);
  
  fvec hist_tmp;
  int samp =0;
  
  
  for (uword act=0; act<group_list.n_rows; act++)
  {
    for (uword gr = 0; gr<24 ; gr++) // 24 gropus for training. 1 for validation
    {
      int training_gp =  cross_val_groups(0,gr);
      std::stringstream tmp_ss;
      
      if (training_gp <10)
      {
	tmp_ss << feat_path<< group_list(act)<< "_0" << training_gp;
      }
      else
      {
	tmp_ss << feat_path << group_list(act)<< "_" << training_gp;
      }
      
      fea_group.load(tmp_ss.str());
      
      //cout << "In " << tmp_ss.str() << " there are " << fea_group.n_rows << " videos"<<endl;
      // All groups per action contains at least 4 videos
      for (uword vi =0; vi<4; vi++) 
      {
	hist_tmp =  conv_to<fvec>::from(fea_group(vi));
	
	cout << "hist_tmp size. Rows: " << hist_tmp.n_rows() << ". Cols: " << hist_tmp.cols() << endl;
	getchar();

	 
          //  samples.push_back(samp);

            // if this point is less than 10 from the origin
            //if (sqrt((double)r*r + c*c) <= 10)
             //   labels.push_back(+1);
	    
	    
	    
	    
	    
	//trainingDataArma.row(samp) = hist_tmp.t();
	//labelsArma(sample) = act + 1; // label = action index
	//samp++;
	
	
	
      }
    }
  }
  
}
